﻿using System;
using System.Collections.Generic;
using System.Data;
using SAS.Core.Entity;
using SAS.Core.Result;

namespace SAS.DataAccess
{
    public class WorkspaceDAO : BaseDAO
    {
        #region Contructors
        protected WorkspaceDAO() : base(string.Empty)
        {

        }
        public WorkspaceDAO(string tokenKey) : base(tokenKey)
        {

        }
        #endregion

        /// <summary>
        /// Get list of workspaces with some custom information.
        /// </summary>
        /// <param name="pageNumber">page number</param>
        /// <param name="pageSize">page size</param>
        /// <param name="orderby">order by column</param>
        /// <param name="conditionList">conditionList</param>
        /// <returns></returns>
        public SASResult<IList<WorkspaceSummary>> GetSummary(int pageNumber, int pageSize, string orderby, string conditionList)
        {
            //Indentify view name
            const string viewName = "[scmAdminApp].[vwWorkSpaceSummary]";

            //Indentify structure that parse SQL results to objects.
            Func<IDataReader, WorkspaceSummary> make = reader => new WorkspaceSummary
                                                                     {
                                                                         WorkspaceId = Convert.ToInt32(reader["pkWorkSpace_ID"]),
                                                                         ClientId = Convert.ToInt32(reader["ClientID"]),
                                                                         ClientName = Convert.ToString(reader["ClientName"]),
                                                                         WorkspaceName = Convert.ToString(reader["WorkSpaceName"]),
                                                                         HostingEnvironmentName = Convert.ToString(reader["HostingEnvName"]),
                                                                         TargetEnvironmentName = Convert.ToString(reader["TargetEnvName"]),
                                                                         WorkSpaceStatusName = Convert.ToString(reader["WorkSpaceStatusName"]),
                                                                         WorkSpaceNote = Convert.ToString(reader["WorkSpaceNote"]),
                                                                         CreatedBy = Convert.ToString(reader["CreatedBy"]),
                                                                         CreatedOn = Convert.ToString(reader["CreatedDate"]),
                                                                         LastModifiedBy = Convert.ToString(reader["WSModifiedBy"]),
                                                                         LastModifiedOn = reader["WSModifiedDate"] == DBNull.Value 
                                                                                                                         ? String.Empty
                                                                                                                         : Convert.ToString(reader["WSModifiedDate"]),
                                                                     };

            //Call to common function to get the data result.
            return Search(pageNumber, pageSize, orderby, conditionList, viewName, make);
        }

        /// <summary>
        /// Create new Workspace. 
        /// </summary>
        /// <param name="workspaceInfo"></param>
        /// <param name="clientInfo"></param>
        /// <param name="sasInfo"></param>
        /// <returns></returns>
        public SASResult<string> Save(string workspaceInfo, string clientInfo, string sasInfo)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspWorkspaceRec_SetByToken]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                                            {                 
                                                new Parameter {Name = "@Token", Value = TokenKey},
                                                new Parameter {Name = "@WorkSpaceXMLParams", Value = workspaceInfo},
                                                new Parameter {Name = "@ClientXMLParams", Value = clientInfo},
                                                new Parameter {Name = "@ClientIOFileVarsXMLParams", Value = sasInfo},
                                                new Parameter
                                                    {
                                                        Name = "@pkWorkSpace_ID",
                                                        ParameterDirection = ParameterDirection.Output,
                                                        OutputReturnName = "WorkspaceId",
                                                        Size = 10
                                                    }
                                            };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }
 
        /// <summary>
        /// Get workspace by workspace Id
        /// </summary>
        /// <param name="workspaceId"></param>
        /// <param name="workspaceSchema"></param>
        /// <param name="clientSchema"></param>
        /// <param name="clientIOFileVarsSchema"></param>
        /// <returns></returns>
        public SASResult<string> Get(int workspaceId, string workspaceSchema, string clientSchema, string clientIOFileVarsSchema)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspWorkSpaceRec_GetByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                                            {
                                                new Parameter {Name = "@Token", Value = TokenKey},
                                                new Parameter {Name = "@pkWorkSpace_ID", Value = workspaceId},
                                                new Parameter
                                                    {
                                                        Name = "@ClientLogo",
                                                        Value = String.Empty,
                                                        OutputReturnName = "ClientLogo",
                                                        ParameterDirection = ParameterDirection.Output,
                                                        DbType = SqlDbType.VarChar,
                                                        Size = Int32.MaxValue
                                                    },
                                                new Parameter
                                                    {
                                                        Name = "@WorkSpaceXMLParams",
                                                        Value = workspaceSchema,
                                                        OutputReturnName = "WorkSpaceInfo",
                                                        ParameterDirection = ParameterDirection.Output,
                                                        DbType = SqlDbType.Xml
                                                    },
                                                new Parameter
                                                    {
                                                        Name = "@ClientXMLParams",
                                                        Value = clientSchema,
                                                        OutputReturnName = "ClientInfo",
                                                        ParameterDirection = ParameterDirection.Output,
                                                        DbType = SqlDbType.Xml
                                                    },
                                                new Parameter
                                                    {
                                                        Name = "@ClientIOFileVarsXMLParams",
                                                        Value = clientIOFileVarsSchema,
                                                        OutputReturnName = "ClientIOInfo",
                                                        ParameterDirection = ParameterDirection.Output,
                                                        DbType = SqlDbType.Xml
                                                    }
                                            };

            //Execute SQL
            return DB.Read<String>(storeProcedureName, null, sqlParameters);
        }

        /// <summary>
        /// Set client logo
        /// </summary>
        /// <param name="workspaceId"></param>
        /// <param name="base64LogoImage"></param>
        /// <returns></returns>
        public SASResult<string> SetClientLogo(int workspaceId, string base64LogoImage)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[uspClientLogo_SetByToken$WorkSpace_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                                            {
                                                new Parameter {Name = "@Token", Value = TokenKey},
                                                new Parameter {Name = "@pkWorkSpace_ID", Value = workspaceId},
                                                new Parameter {Name = "@ClientLogo", Value = base64LogoImage}
                                            };

            //Execute SQL
            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        } 

        /// <summary>
        /// Get workspace name by clientId
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public SASResult<IList<String>> GetWorkspaceNames(int clientId)
        {
            //Indentify SQL store procedure name.
            const string storeProcedureName = "[scmAdminApp].[uspWorkspace_GetNamesByClientID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@ClientID", Value = clientId}
                };
            //Indentify structure that parse SQL results to objects.
            Func<IDataReader, String> make = reader => Convert.ToString(reader["WorkSpaceName"]);

            //Execute SQL
            return DB.ReadList(storeProcedureName, make, sqlParameters);
        }



    }
}